/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import biz.FitnessProgramBO;
import biz.ResultBO;
import biz.UserDetailBO;
import entity.Result;
import entity.UserDetail;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrator
 */
public class MTableModel extends AbstractTableModel {

    private ArrayList data;
    private int mode;
    public static final int RESULT_APP_MODE = 0;
    public static final int ACC_APP_MODE = 1;
    public static final int RESULT_EDIT_MODE = 2;
    public static final int MEMBER_MODE = 3;

    public MTableModel(ArrayList data, int mode) {
        this.data = data;
        this.mode = mode;
    }

    public void removeRow(int index) {
        data.remove(index);
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        switch (mode) {
            case 3: 
                return 4;
            case 2:
            case 0:
                return 5;
            case 1:
                return 7;
            default:
                return 0;
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (mode) {
            case 2:
            case 3:
            case 0: {
            try {
                Result row = (Result) data.get(rowIndex);
                switch (columnIndex) {
                    case 0:
                        return row.getId();
                    case 1:
                        return row.getMark();
                    case 2:
                        return row.getComment();
                    case 3:
                        return new FitnessProgramBO().getName(row.getFitnessProgramsId());
                    case 4:
                        return new UserDetailBO().getUName(row.getUserDetailsId());
                    default:
                        return null;
                }
            } catch (SQLException ex) {
                System.out.println("Error: Database Link Error");
            }
            }
            case 1: {
                UserDetail row = (UserDetail) data.get(rowIndex);
                switch (columnIndex) {
                    case 0:
                        return row.getId();
                    case 1:
                        return row.getName();
                    case 2:
                        return row.getAge();
                    case 3:
                        return row.isSex();
                    case 4:
                        return row.getPhone();
                    case 5:
                        return row.getEmail();
                    case 6:
                        return row.getUsername();
                    default:
                        return null;
                }
            }
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (mode) {
            case 2:
            case 3:
            case 0: {
                switch (column) {
                    case 0:
                        return "ID";
                    case 1:
                        return "Mark";
                    case 2:
                        return "Comment";
                    case 3:
                        return "Fitness Program Program";
                    case 4:
                        return "User Name";
                    default:
                        return null;
                }
            }
            case 1: {
                switch (column) {
                    case 0:
                        return "ID";
                    case 1:
                        return "Name";
                    case 2:
                        return "Age";
                    case 3:
                        return "Female";
                    case 4:
                        return "Phone";
                    case 5:
                        return "Email";
                    case 6:
                        return "Username";
                    default:
                        return null;
                }
            }
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (mode) {
            case 2:
            case 3:
            case 0: {
                switch (columnIndex) {
                    case 0:
                    case 3:
                    case 4:
                        return Integer.class;
                    case 1:
                    case 2:
                        return String.class;
                    default:
                        return Object.class;
                }
            }
            case 1: {
                switch (columnIndex) {
                    case 0:
                    case 2:
                        return Integer.class;
                    case 1:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        return String.class;
                    case 3:
                        return Boolean.class;
                    default:
                        return null;
                }
            }
            default:
                return null;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (mode == 2) {
            switch (columnIndex) {
                case 1:
                case 2:
                    return true;
                default:
                    return false;
            }
        }
        return false;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (mode == 2) {
            try {
                Result item = (Result) data.get(rowIndex);
                switch (columnIndex) {
                    case 1:
                        item.setMark(Integer.parseInt((String) aValue));
                        new ResultBO().editResult(item);
                        break;
                    case 2:
                        item.setComment((String) aValue);
                        new ResultBO().editResult(item);
                        break;
                    default:
                        break;
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null,"Error: Database Link Error","Message Dialog",JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
